package com.animal.controller;

import com.animal.common.Constants;
import com.animal.common.Result;
import com.animal.entity.pojo.AComment;
import com.animal.entity.pojo.AMenu;
import com.animal.entity.pojo.AUser;
import com.animal.entity.vo.UserVo;
import com.animal.service.CommentService;
import com.animal.service.MenuService;
import com.animal.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private CommentService commentService;

    /**
     * 登录
     *
     * @param userVo
     * @return
     */
    @ResponseBody
    @PostMapping("/login")
    public Result login(@RequestBody UserVo userVo) {
        AUser login = userService.login(userVo.getUserName(), userVo.getPassword());
        List<AMenu> menuList = menuService.getMenuList();
        HashMap<String, Object> map = new HashMap<>();
        map.put("user", login);
        map.put("menu", menuList);
        return Result.success(Constants.CODE_200, "登录成功", map);
    }

    /**
     * 展示管理员信息
     *
     * @param userName
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ResponseBody
    @PostMapping(value = {"/getAdmin/{pageNo}/{pageSize}",
            "/getAdmin/{userName}/{pageNo}/{pageSize}",

    })
    public Result getAdmin(@PathVariable(required = false) String userName,
                           @PathVariable Integer pageNo,
                           @PathVariable Integer pageSize) {
        System.out.println(pageNo);
        PageHelper.startPage(pageNo, pageSize);
        List<AUser> admin = userService.getAdmin(userName);
        PageInfo<AUser> pageInfo = new PageInfo<>(admin);
        return Result.success(pageInfo);
    }

    /**
     * 更具id查询具体对象
     *
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/getAdminById/{id}")
    public Result getAdminById(@PathVariable Integer id) {
        AUser aUser = userService.getAdminById(id);
        if (aUser != null) {
            return Result.success(aUser);
        }
        return Result.error();
    }

    /**
     * 修改管理员信息
     *
     * @param user
     * @return
     */
    @ResponseBody
    @PostMapping("/updateAdmin")
    public Result updateAdmin(@RequestBody AUser user) {
        boolean flag = userService.updateAdmin(user);
        if (flag) {
            return Result.success(flag);
        }
        return Result.error();
    }


    /**
     * 增加管理员信息
     *
     * @param user
     * @return
     */
    @ResponseBody
    @PostMapping("/addAdmin")
    public Result addAdmin(@RequestBody AUser user) {
        boolean flag = userService.addAdmin(user);
        if (flag) {
            return Result.success(flag);
        } else {
            return Result.error();
        }
    }

    /**
     * 删除管理员信息
     * 可批量
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @PostMapping("/deleteAdmin")
    public Result deleteAdmin(@RequestBody List<Integer> ids) {
        boolean flag = userService.deleteAdmin(ids);
        if (flag) {
            return Result.success(flag);
        } else {
            return Result.error();
        }
    }


    /**
     * 删除管理员信息
     *
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/deleteAdminById/{id}")
    public Result deleteAdminById(@PathVariable Integer id) {
        boolean flag = userService.deleteAdminByid(id);
        if (flag) {
            return Result.success(flag);
        } else {
            return Result.error();
        }
    }

//    --------------------------用户信息-----------------------------------

    /**
     * 展示用户信息
     *
     * @param userName
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ResponseBody
    @PostMapping(value = {"/getUser/{pageNo}/{pageSize}",
            "/getUser/{userName}/{pageNo}/{pageSize}",

    })
    public Result getUser(@PathVariable(required = false) String userName,
                          @PathVariable Integer pageNo,
                          @PathVariable Integer pageSize) {
        System.out.println(pageNo);
        PageHelper.startPage(pageNo, pageSize);
        List<AUser> admin = userService.getUser(userName);
        PageInfo<AUser> pageInfo = new PageInfo<>(admin);
        return Result.success(pageInfo);
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @ResponseBody
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody AUser user) {
        boolean flag = userService.updateUser(user);
        if (flag) {
            return Result.success(flag);
        }
        return Result.error();
    }

    /**
     * 增加用户信息
     *
     * @param user
     * @return
     */
    @ResponseBody
    @PostMapping("/addUser")
    public Result addUser(@RequestBody AUser user) {
        boolean flag = userService.addUser(user);
        if (flag) {
            return Result.success(flag);
        }
        return Result.error();
    }

    /**
     * 删除用户信息
     * 可批量
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @PostMapping("/deleteUser")
    public Result deleteUser(@RequestBody List<Integer> ids) {
        boolean flag = userService.deleteUser(ids);
        if (flag) {
            return Result.success(flag);
        } else {
            return Result.error();
        }
    }


    /**
     * 删除管理员信息
     *
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/deleteUserById/{id}")
    public Result deleteUserById(@PathVariable Integer id) {
        boolean flag = userService.deleteUserByid(id);
        if (flag) {
            return Result.success(flag);
        }
        return Result.error();
    }

    /**
     * 更具id查询用户对象
     *
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/getUserById/{id}")
    public Result getUserById(@PathVariable Integer id) {
        AUser aUser = userService.getUserById(id);
        if (aUser != null) {
            return Result.success(aUser);
        }
        return Result.error();
    }

    /**
     * 普通用户登录
     *
     * @param
     * @return
     */


    @PostMapping("/loginByUser")
    public String loginByUser(HttpServletRequest request, Model model) {

        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        AUser login = userService.loginByUser(userName, password);
        if (login != null) {
            HttpSession session = request.getSession(true);
            session.setAttribute("user", login);
            model.addAttribute("user", login);
            return "index";
        } else {
            return null;
        }
    }
    //注册

    @RequestMapping(value = "/addUser2",method = RequestMethod.POST)
    public String addUser2(AUser user) {
        boolean flag = userService.addUser2(user);
        return "redirect:/";
    }

    @RequestMapping("/index")
    public String index() {
        return "index";
    }

    @RequestMapping("/activity")
    public String activity() {
        return "activity";
    }

    @RequestMapping("/adopt")
    public String adopt() {
        return "adopt";
    }

    @RequestMapping("/goods")
    public String goods() {
        return "goods";
    }

    @RequestMapping("/personal")
    public String personal() {
        return "personal";
    }

    @RequestMapping("/about")
    public String about() {
        return "about";
    }

    //    个人中心信息修改
    @RequestMapping("/toPersonal")
    public String toPersonal(@RequestParam("id") Integer id, Model model) {
        AUser aUser = userService.getUserById(id);
        String name = aUser.getUserName();
        List<AComment> list = commentService.geiCommentAllByUserName(name);
        model.addAttribute("comList", list);
        model.addAttribute("user", aUser);
        return "personal";
    }

    @RequestMapping(value = "/updatePersonal")
    public String updatePersonal(AUser aUser, HttpServletRequest request, Model model) {
//        System.out.println("xingbie==============================================="+aUser.getSex());
        boolean flag = userService.updateUser(aUser);
        if (flag) {
            AUser login = userService.loginByUser(aUser.getUserName(), aUser.getPassword());
            HttpSession session = request.getSession(true);
            session.setAttribute("user", login);
            model.addAttribute("user", login);
            return "index";
        } else {
            return "personal";
        }
    }

    //    右侧评论
    @RequestMapping("/delCommentById")
    public String delUserById(@RequestParam("id") Integer id,
                              @RequestParam("userId") Integer userId) {

        int count = commentService.deleteById(id);
        if (count > 0) {
            return "redirect:/user/toPersonal?id=" + userId;
        } else {
            return "redirect:/user/";
        }
    }

    @RequestMapping(value = "/updatePersonInfo")
    public String goUpdatePersonInfo(Model model, HttpServletRequest request) {
        //修改页面表单信息填充
        return "";
    }


    @RequestMapping(value = "/doUpdatePersonInfo", method = RequestMethod.POST)
    public void doUpdatePersonInfo(HttpServletRequest request) {
        //修改信息
        String idParameter = request.getParameter("id");
        String sexParameter = request.getParameter("sex");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String address = request.getParameter("address");
        int id = Integer.parseInt(idParameter);
        int sex = Integer.parseInt(sexParameter);
        AUser aUser = new AUser();
        aUser.setAddress(address);
        aUser.setPhone(phone);
        aUser.setEmail(email);
        aUser.setId(id);
        aUser.setSex(sex);
        userService.updateUser(aUser);
    }

}
